package marcis.jarina.directory.repository;

import marcis.jarina.directory.FileActionListener;
import marcis.jarina.directory.FileHelper;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * Author: Jan Marcis
 * Date: 21.1.11
 * Time: 16:18
 */
public class FasterJarProcessor implements FileActionListener {

    private static final Logger log = Logger.getLogger("marcis.jarina.directory.repository.FasterJarProcessor");

    private OptimizedResources jarResources;

    private List<String> searchForClasses;

    private final List<File> foundJars = new ArrayList<File>();

    private int totalFiles;

    private boolean searchAllJars = false;

    //TODO delete explicit dir
    //private static File repositoryDir = new File("C:\\Users\\Jan Marcis\\.m2\\repository\\bsh");
    private static File repositoryDir = new File("C:\\Users\\Jan Marcis\\.m2\\repository\\org");


    @Override
    public void onFileFound(File file) {
        totalFiles++;
        log.debug("Processing: " + file.getName());
        jarResources = new OptimizedResources(file.getPath());
        List<String> classNames = jarResources.getEntries();
        log.info(classNames.toString());
        if (searchAllJars) {
            foundJars.add(file);
        } else {
            if (FileHelper.match(searchForClasses, classNames)) {
                foundJars.add(file);
            }
        }
    }

    public List<String> getEntries() {
        return jarResources.getEntries();
    }

    public void setSearchForClasses(List<String> searchForClasses) {
        this.searchForClasses = searchForClasses;
    }

    public List<File> getFoundJars() {
        return foundJars;
    }

    public int getTotalFiles() {
        return totalFiles;
    }

    public File getRepositoryDir() {
        if (repositoryDir == null) {
            repositoryDir = findRepository();
        }
        return repositoryDir;
    }

    @Override
    public void finish() {
        searchForClasses.clear();
    }

    public static File findRepository() {
        // first check M2_REPO
        if (repositoryDir != null) {
            return repositoryDir;
        }
        File res = checkProp("M2_REPO");
        if (res == null) {
            // user profile dir exists
            res = checkProp("USERPROFILE");
            // check its subdir
            if (res != null) {
                res = new File(res, ".m2/repository");
                if (res.isDirectory() && res.exists()) {
                    return res;
                }
            }
        }
        return res;
    }

    private static File checkProp(String name) {
        if (System.getenv(name) != null && new File(System.getenv(name)).exists()) {
            return new File(System.getenv(name));
        }
        return null;
    }

    public boolean isSearchAllJars() {
        return searchAllJars;
    }

    public void setSearchAllJars(boolean searchAllJars) {
        this.searchAllJars = searchAllJars;
    }
}
